package com.getpebble.android.notifications.model.apps;

import android.app.Notification;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.PblNotificationModel;
import com.getpebble.android.common.model.PblNotificationModel.Record;
import com.getpebble.android.notifications.model.PblNotification;
import com.getpebble.android.notifications.model.PblNotification.NotificationContent;
import com.getpebble.android.notifications.model.PblNotification.Source;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.ArrayList;

public class NotificationExtractor {
    private static final String TAG = NotificationExtractor.class.getSimpleName();
    protected final NotificationContent mContent;
    protected final Context mContext;
    protected final PblNotification mPblNotification;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$notifications$model$PblNotification$Source = new int[Source.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$notifications$model$PblNotification$Source[Source.SMS.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$notifications$model$PblNotification$Source[Source.JSKIT.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$notifications$model$PblNotification$Source[Source.DEMO.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    public static class DuplicateNotificationException extends Exception {
    }

    public NotificationExtractor(PblNotification pblNotification, Context context) {
        if (pblNotification == null) {
            throw new IllegalArgumentException("pblNotification cannot be null");
        } else if (context == null) {
            throw new IllegalArgumentException("context cannot be null");
        } else {
            this.mPblNotification = pblNotification;
            this.mContext = context;
            this.mContent = new NotificationContent();
        }
    }

    public static NotificationContent extractNotificationContent(PblNotification pblNotification, Context context, String groupSummaryKey) throws DuplicateNotificationException {
        NotificationExtractor extractor;
        String pkg = pblNotification.getPackageName();
        boolean isSidechannelled = (pblNotification.isGroupSummary() || pblNotification.getGroupKey() == null) ? false : true;
        if (!isSidechannelled && doesPackageExistInList(MergedMessagesNotificationExtrator.MESSAGING_PACKAGE_NAMES, pkg)) {
            Trace.verbose(TAG, "MergedMessagesNotificationExtrator");
            extractor = new MergedMessagesNotificationExtrator(pblNotification, context);
        } else if (isSidechannelled || !pkg.equals("com.whatsapp")) {
            Trace.verbose(TAG, "NotificationExtractor");
            extractor = new NotificationExtractor(pblNotification, context);
        } else {
            Trace.verbose(TAG, "WhatsAppNotificationExtractor");
            extractor = new WhatsAppNotificationExtractor(pblNotification, context);
        }
        extractor.extractContent();
        extractor.processContent(pblNotification);
        return extractor.getContent();
    }

    private static boolean doesPackageExistInList(ArrayList<String> packageNames, String packageName) {
        if (packageNames == null || packageName == null) {
            return false;
        }
        return packageNames.contains(packageName);
    }

    private NotificationContent getContent() {
        return this.mContent;
    }

    protected void extractContent() {
        Notification notification = this.mPblNotification.getNotification();
        this.mContent.notificationTitle = this.mPblNotification.getApplicationName();
        this.mContent.notificationSummary = null;
        Bundle bundleExtras = NotificationCompat.getExtras(notification);
        if (bundleExtras != null) {
            this.mContent.notificationTitle = bundleExtras.getString("android.title");
            if (this.mContent.notificationTitle == null) {
                this.mContent.notificationTitle = this.mPblNotification.getApplicationName();
            }
            this.mContent.notificationSummary = bundleExtras.getString("android.summaryText");
            if (this.mContent.notificationSummary == null) {
                this.mContent.notificationSummary = bundleExtras.getString("android.subText");
            }
            CharSequence extraBigText = bundleExtras.getCharSequence("android.bigText");
            CharSequence extraText = bundleExtras.getCharSequence("android.text");
            if (extraBigText != null && !"com.google.android.talk".equals(this.mPblNotification.getPackageName())) {
                this.mContent.notificationBody = extraBigText.toString();
            } else if (extraText != null) {
                this.mContent.notificationBody = extraText.toString();
            }
            if (TextUtils.isEmpty(this.mContent.notificationBody)) {
                CharSequence[] textLines = bundleExtras.getCharSequenceArray("android.textLines");
                if (textLines != null) {
                    StringBuffer linesBuffer = new StringBuffer();
                    boolean first = true;
                    for (CharSequence append : textLines) {
                        if (first) {
                            first = false;
                        } else {
                            linesBuffer.append("\n");
                        }
                        linesBuffer.append(append);
                    }
                    this.mContent.notificationBody = linesBuffer.toString();
                }
            }
            if (TextUtils.isEmpty(this.mContent.notificationBody)) {
                CharSequence infoText = bundleExtras.getCharSequence("android.infoText");
                if (infoText != null) {
                    this.mContent.notificationBody = infoText.toString();
                }
            }
        }
        if (TextUtils.isEmpty(this.mContent.notificationBody)) {
            this.mContent.notificationBody = this.mPblNotification.getTickerText();
        }
    }

    private static Record findNotification(String packageName, String text, ContentResolver contentResolver, boolean checkRemovedTimestamp, PblNotification pblNotification) {
        return PblNotificationModel.findPreviousNotification(packageName, text, null, contentResolver, checkRemovedTimestamp, pblNotification);
    }

    protected void processContent(PblNotification pblNotification) throws DuplicateNotificationException {
        if (findNotification(this.mPblNotification.getPackageName(), this.mContent.notificationBody, this.mContext.getContentResolver(), true, pblNotification) != null) {
            Trace.debug(TAG, "Dup of " + this.mPblNotification.getAndroidNotificationId());
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$notifications$model$PblNotification$Source[pblNotification.getSource().ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                case SMTPMessage.RETURN_HDRS /*2*/:
                case ListInfo.INDETERMINATE /*3*/:
                    break;
                default:
                    throw new DuplicateNotificationException();
            }
        }
        Record previous = findNotification(this.mPblNotification.getPackageName(), null, this.mContext.getContentResolver(), false, pblNotification);
        this.mContent.rawNotificationBody = this.mContent.notificationBody;
        if (previous != null) {
            this.mContent.notificationBody = extractNewText(this.mContent.notificationBody, previous.text);
        }
    }

    static String extractNewText(String incomingText, String previousText) {
        Trace.verbose(TAG, "extractNewText new = '" + incomingText + "' previous = '" + previousText + "'");
        if (incomingText == null) {
            return null;
        }
        String resultText = incomingText;
        if (previousText != null) {
            String[] previousLines = previousText.split("\n");
            String[] incomingLines = incomingText.split("\n");
            if (previousText.equals(incomingText)) {
                return resultText;
            }
            int startLine = 0;
            for (String prevLine : previousLines) {
                if (startLine < incomingLines.length && prevLine.equals(incomingLines[startLine])) {
                    startLine++;
                }
            }
            int endLine = incomingLines.length - 1;
            if (startLine == 0) {
                int newLine = endLine;
                while (newLine >= 0) {
                    int oldLine = newLine + (previousLines.length - incomingLines.length);
                    if (oldLine >= previousLines.length || oldLine < 0 || !previousLines[oldLine].equals(incomingLines[newLine])) {
                        break;
                    }
                    endLine--;
                    newLine--;
                }
            }
            if (startLine > 0 || endLine < incomingLines.length - 1) {
                StringBuilder outBuilder = new StringBuilder();
                boolean first = true;
                for (int l = startLine; l <= endLine; l++) {
                    if (first) {
                        first = false;
                    } else {
                        outBuilder.append("\n");
                    }
                    outBuilder.append(incomingLines[l]);
                }
                if (outBuilder.length() > 0) {
                    resultText = outBuilder.toString();
                }
            }
        }
        if (incomingText.equals(resultText)) {
            return resultText;
        }
        Trace.verbose(TAG, "extractNewText(): Replacing '" + incomingText + "' with '" + resultText + "'");
        return resultText;
    }
}
